package cn.huiyunche.admin.controller;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.huiyunche.admin.utils.AdminUtils;
import cn.huiyunche.base.service.interfaces.CWaybillContactsService;
import cn.huiyunche.base.service.model.CWaybillContacts;
import cn.huiyunche.base.service.model.SysAdminUser;
import cn.huiyunche.base.service.vo.Result;

@Controller
@RequestMapping("/contacts")
public class CWaybillContactsController {
	
	@Autowired
	private CWaybillContactsService cWaybillContactsService = null;

	/**
	 * 根据运单、联系人类型查询交接单联系人数据
	 * 
	 * @param waybillId
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/selectcontacts", method = RequestMethod.GET)
	public Result<Object> selectContacts(Long waybillId, int type) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "查询交接单联系人成功");
		List<CWaybillContacts> contacts = this.getCWaybillContactsService().selectContacts(waybillId, type);
		if (CollectionUtils.isNotEmpty(contacts)) {
			result.setData(contacts.get(0));
		}
		return result;
	}
	
	/**
	 * 新增、修改交接单联系人
	 * 
	 * @param contacts
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<Object> add(CWaybillContacts contacts) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "操作已成功");
		SysAdminUser adminUser = AdminUtils.getSysAdminUser();
		result.setData(this.getCWaybillContactsService().add(contacts, adminUser));
		return result;
	}

	private CWaybillContactsService getCWaybillContactsService() {
		return this.cWaybillContactsService;
	}
}
